package com.dj.msgtable.controller;

import com.dj.msgtable.commom.PhoneNumber;
import com.dj.msgtable.commom.TimeGet;
import com.dj.msgtable.entity.Article;
import com.dj.msgtable.entity.Custom;
import com.dj.msgtable.entity.User;
import com.dj.msgtable.service.ArticleService;
import com.dj.msgtable.service.CustomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ArticleController {


    @Autowired
    ArticleService articleService;

    @PostMapping("/addArticle")
    @ResponseBody
    public String addCustombyhand(@RequestBody Article article, HttpServletRequest request) throws IOException {
//        customService.changeState(custom);
        HttpSession session = request.getSession();
        Object object =  session.getAttribute("user");

//        if (object!=null) {
            Long time = TimeGet.nowTimeLong();
            String date = TimeGet.nowTimeSteing();
            article.setDate(date);
            article.setTime(time);
            System.out.println(article.toString());
            articleService.addArticle(article);
//        }

        return "增加成功";
    }

    @PostMapping( "/ArticleList")
    @ResponseBody
    public List<Article> searchUserList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        Object object =  session.getAttribute("user");
//        if (object!=null){
            User user = (User) object;
            List<Article> articles =articleService.selectArticle();
            return articles;
//        }
//        return null;
    }
}
